package com.zhuoxin.dao.admin;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.model.Course;
import com.zhuoxin.bean.model.CourseSO;
import com.zhuoxin.bean.model.Teacher;
import com.zhuoxin.bean.system.Category;
import com.zhuoxin.dao.BaseDAOImpl;

public class CourseDAOImpl extends BaseDAOImpl<Course, CourseSO> implements CourseDAO {

	@Override
	public String getHqlWhere(CourseSO so) {
		return null;
	}

	@Override
	public void setCriteriaParams(Criteria criteria, CourseSO so) {
		if(so.getCategory()!=null){
			criteria.createAlias("category", "c");
			if(so.getCategory().getId()!=null){
				criteria.add(Restrictions.eq("c.id", so.getCategory().getId()));
			}
			
			if(StringUtils.isNotEmpty(so.getCategory().getName())){
				criteria.add(Restrictions.ilike("c.name", "%"+StringUtils.trim(so.getCategory().getName())+"%"));
			}
			
			if(StringUtils.isNotEmpty(so.getCategory().getCode())){
				criteria.add(Restrictions.eq("c.code", StringUtils.trim(so.getCategory().getCode())));
			}
			
		}
		
		if(so.getTeacher()!=null){
			criteria.createAlias("teacher", "t");
			
			if(so.getTeacher().getId()!=null){
				criteria.add(Restrictions.eq("t.id", so.getTeacher().getId()));
			}
			
			if(StringUtils.isNotEmpty(so.getTeacher().getName())){
				criteria.add(Restrictions.ilike("t.name", "%"+StringUtils.trim(so.getTeacher().getName())+"%"));
			}
			
			if(StringUtils.isNotEmpty(so.getTeacher().getCode())){
				criteria.add(Restrictions.eq("t.name", StringUtils.trim(so.getTeacher().getCode())));
			}
			
			if(StringUtils.isNotEmpty(so.getTeacher().getIdcard())){
				criteria.add(Restrictions.eq("t.idcard", StringUtils.trim(so.getTeacher().getIdcard())));
			}
			
		}
		
		if(StringUtils.isNotEmpty(so.getCode())){
			criteria.add(Restrictions.eq("code", StringUtils.trim(so.getCode())));
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			criteria.add(Restrictions.ilike("name", "%"+StringUtils.trim(so.getName())+"%"));
		}
		
		if(so.getIsHot()!=null){
			criteria.add(Restrictions.eq("isHot", so.getIsHot()));
		}
		
		criteria.add(Restrictions.eq("flag", true));
		criteria.add(Restrictions.eq("status", true));
	}

	@Override
	public void setQueryParams(Query query, CourseSO so) {
		
	}

	@Override
	public void switchVO2PO(Course po, CourseSO so) {
		if(StringUtils.isNotEmpty(so.getCode())){
			po.setCode(StringUtils.trim(so.getCode()));
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			po.setName(StringUtils.trim(so.getName()));
		}
		
		if(StringUtils.isNotEmpty(so.getCaption())){
			po.setCaption(StringUtils.trim(so.getCaption()));
		}
		
		if(StringUtils.isNotEmpty(so.getPhoto())){
			po.setPhoto(StringUtils.trim(so.getPhoto()));
		}
		
		if(so.getTuition()!=null){
			po.setTuition(so.getTuition());
		}
		
		if(so.getIsHot()!=null){
			po.setIsHot(so.getIsHot());
		}
		
		if(so.getCategory()!=null && so.getCategory().getId()!=null){
			Category category=findById(Category.class, so.getCategory().getId());
			po.setCategory(category);
		}
		
		if(so.getTeacher()!=null && so.getTeacher().getId()!=null){
			Teacher teacher=findById(Teacher.class, so.getTeacher().getId());
			po.setTeacher(teacher);
		}
		
	}
	
	public <T> void deleteById(Serializable id){
		Session session=getSessionFactory().getCurrentSession();
		Transaction transaction=session.beginTransaction();
		Query query=session.createQuery("delete from GradeCourse where course.id=:courseId");
		query.setParameter("courseId", id);
		query.executeUpdate();
		
		query=session.createQuery("delete from Grade where id=:id");
		query.setParameter("id", id);
		query.executeUpdate();
		transaction.commit();
	}
}
